#coding:utf-8

import scrapy

from proxy.items import ProxyItem

class ProxySpider(scrapy.Spider):
	name = 'xicidaili'
	allowed_domains = ['xicidaili.com']

	"""start_urls = [
		"http://www.xicidaili.com/nn/1",
		"http://www.xicidaili.com/wn/1",
		"http://www.xicidaili.com/nn/2",
		"http://www.xicidaili.com/wn/2",
		"http://www.xicidaili.com/nn/3",
		"http://www.xicidaili.com/wn/3",
		"http://www.xicidaili.com/nn/4",
		"http://www.xicidaili.com/wn/4",
		"http://www.xicidaili.com/nn/5",
		"http://www.xicidaili.com/wn/5",
	]"""

	def start_requests(self):
		proxyType = ['nn','wn','nt','wt']
		for index in range(1,3):
			for ptype in proxyType:
				yield scrapy.Request('http://www.xicidaili.com/%s/%d' %(ptype, index),self.parse)


	def parse(self, response):
		contents = response.xpath("//table/tr/td")
		for i in range(len(contents)/10):
			item = ProxyItem()
			item['ip'] = contents[i*10+1].xpath("text()").extract()[0]
			item['port'] = contents[i*10+2].xpath("text()").extract()[0]
			item['loc'] = contents[i*10+3].xpath("./a/text()").extract()[0]
			item['ptype'] = contents[i*10+4].xpath("text()").extract()[0]
			item['time'] = contents[i*10+9].xpath("text()").extract()[0]
			yield item
			#print item['ip']+":"+item['port']
